#include <cstdio>
#include "glad/glad.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include "GUI.hpp"

GUI::GUI(Engine* engine_)
{
	engine = engine_;
	ImGui::CreateContext();
	ImGui_ImplGlfw_InitForOpenGL(engine->camera->window, true);
	ImGui_ImplOpenGL3_Init("#version 300 es");
}
void GUI::draw()
{
	ImGui_ImplOpenGL3_NewFrame();
	ImGui_ImplGlfw_NewFrame();
	ImGui::NewFrame();



	ImGui::Begin("Simple Game Engine", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground);
	{
		ImGui::SetWindowSize(ImVec2(1280.f, 0.f));
		ImGui::SetWindowPos(ImVec2(0.f, 0.f));
		ImGui::BeginMenuBar();
		{
			if (ImGui::BeginMenu("File"))
			{
				ImGui::MenuItem("Open", "Ctrl+O");
				ImGui::MenuItem("Save", "Ctrl+S");
				ImGui::MenuItem("Quit", "Ctrl+Q");
				ImGui::EndMenu();
			}
		}
		ImGui::EndMenuBar();
	}
	ImGui::End();



	ImGui::Begin("FPS", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar);
	{
		ImGui::SetWindowSize(ImVec2(40.f, 30.f));
		ImGui::SetWindowPos(ImVec2(0.f, 690.f));
		ImGui::Text("%d", static_cast<int>(floorf(1.0f / engine->period)));
	}
	ImGui::End();



	/*ImGui::Begin("SceneTree", nullptr, ImGuiWindowFlags_AlwaysVerticalScrollbar);
	{*/
		if (ImGui::TreeNode("Tree"))
		{
			auto f = engine->world_ecs.filter_builder<vec3>()
				.term_at(1)
				.second< tagPosition>()
				.build();
			f.each([](flecs::entity e, vec3& m)
			{
				e.children([](flecs::entity e)
				{
					if (ImGui::TreeNode(e.name().c_str()))
					{
						ImGui::TreePop();
					}
				});
			});

			ImGui::TreePop();
		}
	/*}
	ImGui::End();*/



	ImGui::Render();
	ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
GUI::~GUI()
{
	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplGlfw_Shutdown();
	ImGui::DestroyContext();
}
